package com.k.demo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.k.demo.entity.Food;
import com.k.demo.entity.FoodKind;
import com.k.demo.entity.GoodOrderDetail;
import com.k.demo.service.FoodKindService;
import com.k.demo.service.FoodService;
import com.k.demo.service.GoodOrderDetailService;

@RestController
@RequestMapping("/goodorderdetail")
public class GoodOrderDetailController {

	@Autowired
	private GoodOrderDetailService goodOrderDetailService;
	@Autowired
	private FoodService foodservice;
	@Autowired
	private FoodKindService foodKindService;
	
	
	@RequestMapping("/getorderdetail/{order_id}")
	public Object getAllDetail(@PathVariable("order_id")Integer order_id) {
		
		System.out.println("获取订单详情信息:"+order_id);
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("usergoodorderlist", goodOrderDetailService.getDetails(order_id));
			
		return map;
		
	}
	@RequestMapping("/getfoodmonthsalenumber")
	public Object getMonthSale(@RequestParam int food_id) {
			
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("foodmonthsalenum", goodOrderDetailService.getFoodMonthSaleNumber(food_id));
			
		return map;
		
	}
	//菜品销售总量
	@RequestMapping("/getsalenumberbydate")
	public Object getSaleNumberByDate(@RequestParam String date) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("salenumber", goodOrderDetailService.getSaleNumberByDate(date));
		
		return map;
	}
	@RequestMapping("/getfoodsalenumberbykind")
	public Object getFoodSaleNumberByKind(@RequestBody HashMap<String, Object> map) {
		
		List<HashMap<String, Object>> rmap = new ArrayList<HashMap<String, Object>>();
		
		rmap=(List<HashMap<String, Object>>) goodOrderDetailService.getFoodSaleNumberByKind((String)map.get("start_date"),(String)map.get("end_date"));
		
		return rmap;
	}
	//每样菜品销售量
	@RequestMapping("/getfoodsalenumberbydate")
	public Object getFoodSaleNumberByDate(@RequestParam String date) {
		
		Map<String,Object> map = new HashMap<String, Object>();
		List<HashMap<String, Object>> food_kind = goodOrderDetailService.getFoodSaleNumberByKind((String)map.get("start_date"),(String)map.get("end_date"));
		List<GoodOrderDetail> detail = goodOrderDetailService.getFoodSaleNumber(date);
		String[] foodname = new String[detail.size()];
		Integer[] salenum = new Integer[detail.size()]; 
		if(detail.size()!=0) {	 			
			for(int i =0;i<detail.size();i++) {
				foodname[i]=detail.get(i).getFood_name();
				salenum[i]=detail.get(i).getFood_amount();				
			}
			map.put("foodname", foodname);
			map.put("salenum", salenum);
			return map;
		}else {
			map.put("foodname", foodname);
			map.put("salenum", salenum);
			return map;
		}
		
			
		
	}
	@RequestMapping("/getfoodsalenumberbytime")
	public Object getFoodSaleNumberByTime(@RequestBody HashMap<String, Object> map) {
		List<List<List<Object>>> alldata =new ArrayList<List<List<Object>>>();
		List<List<Object>> data = new ArrayList<List<Object>>();
		List<List<Object>> foodkinddata=new ArrayList<List<Object>>();
		List<HashMap<String, Object>> food_kind_data = (List<HashMap<String, Object>>) goodOrderDetailService.getFoodSaleNumberByKind((String)map.get("start_date"),(String)map.get("end_date"));
		List<FoodKind> food_kind = foodKindService.getAll();
		List<Food> food = foodservice.getAll();
		List<GoodOrderDetail> detail = goodOrderDetailService.getFoodSaleNumberByTime((String)map.get("start_date"), (String)map.get("end_date"));
		for(FoodKind i:food_kind) {
			List<Object> temp =  new ArrayList<Object>();
			temp.add(0);
			temp.add(0.0);
			temp.add(100000000);
			temp.add(i.getFood_kind_name());
			foodkinddata.add(temp);
			
			
		}
		if(food_kind_data.size()!=0) {
			
			for(int l=0;l<food_kind.size();l++) {
				
				for(int k=0;k<food_kind_data.size();k++) {
					System.out.println(food_kind_data);
					if(food_kind_data.get(k).get("NAME").equals(food_kind.get(l).getFood_kind_name())) {
						
						foodkinddata.get(l).set(0,food_kind_data.get(k).get("AMOUNT"));
						foodkinddata.get(l).set(1,food_kind_data.get(k).get("PRICE"));
						foodkinddata.get(l).set(3,food_kind_data.get(k).get("NAME"));
					}
				}
			}
			
		}
		for(Food i:food) {
			List<Object> temp =  new ArrayList<Object>();
			temp.add(0);
			temp.add(0.0);
			temp.add(100000000);
			temp.add(i.getFood_name());
			data.add(temp);
		}
		if(detail.size()!=0){
			
			for(int i =0; i<food.size();i++) {
				
				for(GoodOrderDetail g : detail) {
					
					if(food.get(i).getFood_name().equals(g.getFood_name())) {
						System.out.println(g);
						data.get(i).set(0, g.getFood_amount());
						data.get(i).set(1, g.getFood_total());
						data.get(i).set(3, g.getFood_name());
					}
				}
			}
			
		}
		alldata.add(data);
		alldata.add(foodkinddata);
		return alldata;
			
							
	}
			
}
